
var key = "";
document.onkeydown = monitorKeyboard;

function monitorKeyboard(event){

	var tempKey = event.keyCode;	

	if (tempKey >= 65 && tempKey <= 90) {
		var c = String.fromCharCode(97 + tempKey - 65);
		key = key.concat(c);
	}
	else if (tempKey == 17) {
		key += "*";
	}
	
	var l1 = key.charCodeAt(2);
	var l2 = key.charCodeAt(3);
	var a = 0;
	var b = 0;
	a = isChar(l1);
	b = isChar(l2);
	
	//control + a 
	//mark all links
	if (key == "*a"&& key.length==2) {
		var link = document.getElementsByTagName("a");	
		addMark(link);
		key = "*a";				

	}

	//open corresponding link
	else if (key.substring(0, 2) == "*a" && a==1 && b==1 && key.length == 4){
     	var link = document.getElementsByTagName("a");	
		removeMark(link);
		addMark(link);

		var s = (l1-97)*26+(l2-96);
		if (s <= link.length) 
			openLink(link[s]);
    	key="*a";
    	
    } 

    //deal with mistaken command 
    else if (key.substring(0, 2) == "*a" && a==1 && b==0 && key.length == 4){	
		var link = document.getElementsByTagName("a");	
		removeMark(link);
		addMark(link);
		key="*a";

    }

	// control + a (twice)
	// cancel all marks
	else if (key.substring(0, 3) == "*a*" && key.length == 4) {
		var link = document.getElementsByTagName("a");	
		removeMark(link);
		key = "";

	}

	// to the top
    else if (key == "*q" && key.length == 2) {
    	window.scrollTo(0,0);
    	key = "";
    } 

    // to the bottom
    else if (key == "*e" && key.length == 2) {
    	var h = document.body.scrollHeight;
    	window.scrollTo(0, h);
    	key = "";
    }

    // go up
    else if (key == "*w" && key.length == 2) {
    	window.href = goUp();
    	key = "";
    } 

    // go down
    else if (key == "*s" && key.length == 2) {
    	window.href = goDown();
    	key = "";
    } 

	// reopen the last closed tab
    else if (key == "*r" && key.length == 2) {
	chrome.runtime.sendMessage({greeting: "Z"}, function(response) {
        console.log(response.farewell);
    });
    key = "";
    } 

	// move to the left tab
	else if (key == "*p" && key.length == 2) {
	chrome.runtime.sendMessage({greeting: "P"}, function(response) {
        console.log(response.farewell);
    });
    key = "";
    } 

    // move to the right tab
	else if (key == "*o" && key.length == 2) {
	chrome.runtime.sendMessage({greeting: "O"}, function(response) {
        console.log(response.farewell);
    });
    key = "";
    } 
    
    // reload current tab
	else if (key == "*i" && key.length == 2) {
	chrome.runtime.sendMessage({greeting: "C"}, function(response) {
        console.log(response.farewell);
    });
    key = "";
    } 

    // reload all tabs
	else if (key == "*u" && key.length == 2) {
	chrome.runtime.sendMessage({greeting: "U"}, function(response) {
        console.log(response.farewell);
    });
    key = "";
    } 

    // remove current tab
	else if (key == "*y" && key.length == 2) {
	chrome.runtime.sendMessage({greeting: "S"}, function(response) {
        console.log(response.farewell);
    });
    key = "";
    }

    // remove all tabs
	else if (key == "*t" && key.length == 2) {
	chrome.runtime.sendMessage({greeting: "M"}, function(response) {
        console.log(response.farewell);
    });
    key = "";
    }  

    else if (key.substring(0, 1)!= "*"){
    	key = "";
    }

    else if (key.substring(0, 1)=="*" && key.length == 2) {
    	alert("No such command: CTRL + " + key.substring(1, 2));
    	key = "";
    }

}

function addMark(link){	
	for (var i = 0; i < link.length; i++) {
		var popup = document.createElement("div");

		popup.id = i;
		document.body.appendChild(popup);
					
		popup.style.position = "absolute";
		popup.style.top = getElementTop(link[i]) - 15 + "px";
		popup.style.left = getElementLeft(link[i]) - 10 + "px";	
		popup.style.height = 10 + "px";

		var mark = String.fromCharCode(65+((i-1)/26), 65+((i-1)%26));
		popup.innerHTML = "<p class='hannotate'>" + mark + "</p>";
	
	}
}

function removeMark(link){			
	for (var i = 0; i < link.length; i++) {
		var popup = document.getElementById(i);
		document.body.removeChild(popup);
	
	}
}

function isChar(l){	
	if (l > 96 && l < 123)
		return 1;
	else
		return 0;
}

function openLink(e){	
	var url = e.href;
	window.open(url);

}

function getElementLeft(e){	
	var actualLeft = e.offsetLeft;
	var current = e.offsetParent;

	while (current != null){
		actualLeft += current.offsetLeft;
		current = current.offsetParent;
	}

	return actualLeft;
}

function getElementTop(e){	
	var actualTop = e.offsetTop;
	var current = e.offsetParent;

	while (current != null){
		actualTop += current.offsetTop;
		current = current.offsetParent;
	}

	return actualTop;
}

function goUp(){
	window.scrollBy(0, -100);
	var scrolldelay = setTimeout('goUp()', 100);
	if(document.documentElement.scrollTop == 0) 
		clearTimeout(scrolldelay);
}

function goDown(){
	window.scrollBy(0, 100);
	var scrolldelay = setTimeout('goDown()', 100);
	if(document.documentElement.scrollTop == 0) 
		clearTimeout(scrolldelay);
}
